<template>
  <view>
    <nav-bar title="新增地址" backgroundColor="#115BAA" titleColor="#FFF"></nav-bar>
    <scroll-view scroll-y :style="{ height: scrollHeight }">
      <view class="content-box">
        <view class="form-box">
          <you-form
            ref="form"
            :showFormTitle="false"
            :formConfig="addressConfig"
            v-model="addressData"
          ></you-form>
          <view class="default-box" @click="clickDefault">
            <uni-icons class="icon" v-if="chooseDefault" type="checkbox-filled" color="#2A82E4" size="18"></uni-icons>
            <uni-icons class="icon" v-else type="checkbox" color="#999" size="18"></uni-icons>
            设为默认地址
          </view>
        </view>
      </view>
    </scroll-view>
    <view class="form_bottom_btn">
      <view class="btn-box">
        <view class="confirm" @click="clickConfirmAdd">确认新增</view>
      </view>
    </view>
  </view>
</template>

<script>
  import api_user from '@/api/user'
  import { ADDRESS_FORM } from '../formConfig/addressConfig'
  export default {
    name: 'AddAddress',
    data() {
      return {
        scrollHeight: '',
        addressConfig: [],
        addressData: {},
        chooseDefault: false
      }
    },

    onLoad(options) {
      this.scrollHeight = this.$store.getters.heightData.noTabMainHeight - 66 + 'px'
      this.addressConfig = uni.$u.deepClone(ADDRESS_FORM)
    },

    methods: {
      clickDefault() {
        this.chooseDefault = !this.chooseDefault
      },
      clickConfirmAdd() {
        const params = {
          ...this.addressData,
          isDefault: this.chooseDefault ? 1 : 2
        }
        api_user.addAddress(params).then(res => {
          if (res.code !== 200) return
          uni.showToast({
            title: '新增成功！',
            icon: 'success',
            mask: true
          })
          uni.redirectTo({
            url: `/subPages/userIndex/rehabilitationAids/goodsAddress`
          })
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .content-box {
    padding-top: 30rpx;
    .form-box {
      margin: 0 30rpx;
      border-radius: 16rpx;
      background-color: #FFF;
      overflow: hidden;
      .default-box {
        display: flex;
        align-items: center;
        padding: 0 30rpx;
        min-height: 90rpx;
        font-size: 28rpx;
        color: #606266;
        .icon {
          margin-right: 10rpx;
        }
      }
    }
  }
</style>
